package com.foolishbird.sdk.handler;

import com.foolishbird.sdk.params.ResponseParams;
import com.foolishbird.sdk.utils.WL;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;


public abstract class SimpleAsyncResponseHandler<T extends ResponseParams> extends AsyncResponseHandler {

    private static final String TAG = SimpleAsyncResponseHandler.class.getSimpleName();

    private final Class<T> responseParams;

    public SimpleAsyncResponseHandler(Class<T> responseParams) {
        if (responseParams == null) {
            throw new NullPointerException("Parameter responseParams can not be null.");
        }
        this.responseParams = responseParams;
    }

    public Class<T> getResponseClass() {
        return responseParams;
    }

    @Override
    public void onAsyncResponseSuccess(int flag, String response) {
        try {
//            byte[] src =Base64.decode(response,Base64.DEFAULT);
//            WL.e(TAG, "onAsyncResponseSuccess ---> response" + response);
//            byte[] aaa = RSAUtil.decryptByPrivateKey(src);
//            response = new String(aaa,"UTF-8");
            onAsyncResponseSuccess(flag, new Gson().fromJson(response, responseParams));
            WL.e(TAG, "onAsyncResponseSuccess ---> response111" + response);
        } catch (JsonSyntaxException e) {
            WL.e(TAG, "onAsyncResponseSuccess ---> GSON transition exception " + e.getMessage());
            onAsyncException(flag, e);
        }catch (Exception e2){
            e2.printStackTrace();
        }
    }

    @Override
    public void onAsyncCancel(int flag) {
        WL.d(TAG, "onAsyncCancel ---> flag: " + flag);
    }

    @Override
    public void onAsyncSendRequest(int flag) {
        WL.d(TAG, "onAsyncSendRequest ---> flag: " + flag);
    }

    @Override
    public void onAsyncException(int flag, Exception ex) {
        WL.d(TAG, "onAsyncException ---> flag: " + flag);
    }

    @Override
    public void onAsyncNetworkException(int flag, Exception ex) {
        WL.d(TAG, "onAsyncNetworkException ---> flag: " + flag);
    }

    public abstract void onAsyncResponseSuccess(int flag, T response);
}
